var form = layui.form,
    id=getQueryVariable("id");



var productCode=parent.code;
var specificationCode=parent.fjCode;

//货柜型号
let containerModel;
//获取货柜型号
getContainerModel();
//当前时间
let nowTime=getNowFormatDate();
    //表单初始值，修改时调用
    if(id!=""){
        var oldData =  parent.table.cache["boxGaugeTableId"];
        $.each(oldData, function (index, value) {
            if (oldData[index]["id"] == id) {
                form.val("editor-form",{
                    "productCode":oldData[index]["productCode"],
                    "specificationCode":oldData[index]["specificationCode"],
                    "containerCode":oldData[index]["containerCode"],
                    "containerName":oldData[index]["containerName"],
                    "boxNum":oldData[index]["boxNum"],
                    "boxGrossWeight":oldData[index]["boxGrossWeight"],
                    "boxNetWeight":oldData[index]["boxNetWeight"],
                    "boxNumPallet":oldData[index]["boxNumPallet"],
                    "numPallet":oldData[index]["numPallet"],
                    "createDate":oldData[index]["createDate"],
                    "updateDate":nowTime,
                })
            }
        });
        form.render();
    }else{
        form.val("editor-form",{
            "productCode":productCode,
            "specificationCode":specificationCode,
            "containerTypeCode":"",
            "boxNum":0,
            "boxGrossWeight":0,
            "boxNetWeight":0,
            "boxNumPallet":0,
            "numPallet":0,
            "createDate":nowTime,
            "updateDate":null,
        })
        form.render();
    }
//监听提交
form.on('submit(form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    if(id !="") data.field.id=id;
    $.ajax({
        type : 'post',
        data:JSON.stringify(data.field),
        url:'boxGauge/saveBoxGauge',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                layer.msg("保存成功", {icon: 6});
                //保存成功关闭此页面
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.refresh_box();
            }else {
                layer.msg('提交失败');
            }
        },
    });
    return false;
});
//监听货柜型号下拉框
form.on("select(containerFilter)", function(data){
    if(data.value === ""){
        $('#containerName').val("");
        return;
    }
    for(let i in containerModel){
        if(data.value == containerModel[i].code){
            $('#containerName').val(containerModel[i].name);
        }
    }
});

//获取货柜型号
function getContainerModel(){
    $.ajax({
        url:"offer/getContainerModel",
        async:false,
        success:function(data){
            if(data.status == 200){
                containerModel = data.data;
                for(let i in data.data){
                    $('#containerCode').append(new Option(data.data[i].name, data.data[i].code));
                }
                form.render('select');
            }
        }
    });
}
//时间处理 获取当前时间yyyy-MM-dd hh:mm:ss
function getNowFormatDate() {
    let date = new Date();
    let separator = "-";
    let separatorT=":";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    let hour=date.getHours();
    let minute=date.getMinutes();
    let second = date.getSeconds();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    if (minute >= 0 && minute <= 9) {
        minute = "0" + minute;
    }
    if (second >= 0 && second <= 9) {
        second = "0" + second;
    }
    let currentDate = year + separator + month + separator + strDate+" "+hour+separatorT+minute+separatorT+second;
    return currentDate;

}
